﻿$(function () {
    $('.date').mask('11/11/1111');
    $('.date').datepicker({ dateFormat: 'dd/mm/yy' });
    $('.time').mask('00:00:00');
    $('.date_time').mask('99/99/9999 00:00:00');
    $('.cep').mask('99999-999');
    $('.phone').mask('9999-9999');
    $('.cellphone').mask('99999-9999');
    $('.cellphone_with_ddd').mask('(99) 99999-9999');
    $('.phone_with_ddd').mask('(99) 9999-9999');
    $('.phone_us').mask('(999) 999-9999');
    $('.mixed').mask('AAA 000-S0S');
});

function clearFormContent() {
    $("Form").trigger('reset');
}

function clearTextContent() {
    $("input[type='text']").val('');
}

function clearSelectContent() {
    $("select").val('-');
}

function clearAllContentExceptHidden() {
    clearSelectContent();
    clearTextContent();
}

function validateNumericField(event) {
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
            (event.keyCode == 65 && event.ctrlKey === true) ||
            (event.keyCode >= 35 && event.keyCode <= 39)) {
        return;
    }
    else {
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.preventDefault();
        }
    }
}

//window.alert = function (message) {
//    $(document.createElement('div'))
//        .attr({ title: 'Alert', 'class': 'alert' })
//        .html(message)
//        .dialog({
//            buttons: { OK: function () { $(this).dialog('close'); } },
//            close: function () { $(this).remove(); },
//            draggable: true,
//            modal: true,
//            resizable: false,
//            width: 'auto'
//        });
//};


var alertConfirmCallBackOk;
var alertConfirmCallBackCancel;

window.alert = function (txt, callBackOK) {
    alertConfirmCallBackOk = callBackOK;
    buildAlertConfirmHTML(txt, "Alert");

    $("#alert").dialog("option", "buttons", { "Ok": onCallBackOK });
    $("#alert").dialog('open');
}

//function confirm(txt, callBackOK, callBackCancel) {
//    alertConfirmCallBackOk = callBackOK;

//    if (!callBackCancel) {
//        callBackCancel = function () { return false; };
//    }

//    alertConfirmCallBackCancel = callBackCancel;

//    buildAlertConfirmHTML(txt, "Confirme");

//    $("#alert").dialog("option", "buttons", { "Ok": onCallBackOK, "Cancelar": onCallBackCancel });
//    $("#alert").dialog('open');
//}

function onCallBackOK() {
    onCallBack(alertConfirmCallBackOk);
}

function onCallBackCancel() {
    onCallBack(alertConfirmCallBackCancel);
}

function onCallBack(callBack) {
    if (typeof (callBack) == 'string') {
        window.location.href = callBack;
    }
    else {
        $("#alert").dialog('close');

        if (typeof (callBack) == 'function') {
            callBack();
        }
    }
}

function buildAlertConfirmHTML(txt, title) {
    if ($("#alert").length == 0) {
        $("<div>").attr("id", "alert").insertAfter("body");
        $("<p>").appendTo("#alert");
    }

    $("#alert p").html(txt, title);

    $("#alert").dialog({
        modal: true,
        autoOpen: false,
        closeOnEscape: false,
        title: title
    });
}

function browserVersion() {
    var version = 999;
    if (navigator.appVersion.indexOf("MSIE") != -1)
        version = parseFloat(navigator.appVersion.split("MSIE")[1]);

    return version;
}